//
//  SelectLanguageManager.m
//  BTCBOX
//
//  Created by Apple on 2017/11/24.
//  Copyright © 2017年 jianfengChen. All rights reserved.
//

#import "LanguageManager.h"
#import "AppDelegate.h"
#import "config.h"
#import "NetworkHelper.h"
#import "UserDefaultsManager.h"
#import "CookieManager.h"
@implementation SelectLanguageItem
@end

@implementation LanguageManager
static LanguageManager *instance = nil;
+ (LanguageManager *)sharedManager
{
    static dispatch_once_t once_token;
    dispatch_once(&once_token, ^{
        instance = [[LanguageManager alloc] init];
    });
    return instance;
}
- (void)getAllLanguage:(GetAllLanCallback)getAllLanCallback
{
    NSMutableArray *allLan = [[NSMutableArray alloc] init];
    NSString *path = [[NSBundle mainBundle] pathForResource:kLanguagePath ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSArray *icons = @[[UIImage imageNamed:@"Language_System"],[UIImage imageNamed:@"Language_EN"],[UIImage imageNamed:@"Language_Jan"],[UIImage imageNamed:@"Language_Hans"]];
    SelectLanguageItem *systemItem = [[SelectLanguageItem alloc] init];
    systemItem.lanName = stringWithKey(@"Follow System", nil);
    systemItem.lanPre = [self getCurrentSystemLanguage];
    systemItem.iconImage = icons[0];
    [allLan addObject:systemItem];
    for (NSInteger i = 0;i < arr.count;i++) {
        SelectLanguageItem *item = [[SelectLanguageItem alloc] init];
        item.lanName = [arr[i] allKeys][0];
        item.lanPre = [arr[i] allValues][0];
        item.iconImage = icons[i+1];
        [allLan addObject:item];
    }
    
    getAllLanCallback([allLan copy]);
}
#pragma -mark publicMethod
- (NSString *)getCurrentLanguage
{
    NSString *tmp = nil;
    NSArray *preferredLanguages = [NSLocale preferredLanguages];
    tmp = preferredLanguages[0];
    if ([UserDefaultsManager getLanguage]) {
        tmp = [UserDefaultsManager getLanguage];
    } else {
        tmp = [self defaultLanguagePre];
    }
    return [self getLanguagePre:tmp];
}

- (NSString *)defaultLanguagePre {
    
    return @"zh-Hans";
    
}

//获取当前系统语言
- (NSString *)getCurrentSystemLanguage
{
    NSArray *preferredLanguages = [NSLocale preferredLanguages];
    NSString *tmp = preferredLanguages[0];
    return [self getLanguagePre:tmp];
}
#pragma -mark privateMethod
- (NSString *)getLanguagePre:(NSString *)temp
{
    
    if ([temp hasPrefix:kLanguage_Ja]) {
        temp = kLanguage_Ja;
    }
    else if ([temp hasPrefix:kLanguagePre_en]) {
        temp = kLanguagePre_en;
    }
    else if ([temp hasPrefix:kLanguagePre_zh_Hans]) {
        temp = kLanguagePre_zh_Hans;
    }
    else if([temp hasPrefix:kLanguagePre_zh_Hant])
    {
        temp = kLanguagePre_zh_Hant;
    }
    else {
        temp = kLanguagePre_en;//默认成英文
    }
    return temp;
//    return kLanguagePre_zh_Hans;//非多语言
}
- (NSString *)getLanguage:(NSString *)key
{
    NSString *path = [NSString stringWithFormat:@"%@",[UserDefaultsManager getLanguage]];
    NSString *bundlepath = [[NSBundle mainBundle] pathForResource:path ofType:@"lproj"];
    NSString *s = [[NSBundle bundleWithPath:bundlepath] localizedStringForKey:(key) value:@"" table:@"InfoPlist"];
    return s;
}
@end
